﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

using My_TV_Manager.Model;
using My_TV_Manager.View;
using My_TV_Manager.View.ContentPanel.Track;

namespace My_TV_Manager
{
    public class Controller
    {
        #region Private Data

        // Model Data
        private List<SearchResult> trackedSeries;
        private List<SearchResult> currentSearchResults;

        // Model Settings
        private int initialPanelIndex;
        private int logoPanelIndex;
        private SearchResultSortCategory searchTabSortCategory;
        private bool restrictSearchResults;
        private int searchResultLimit;

        // View Data
        private TrackSeriesPanel trackSeriesPanel;
        private TrackSearchPanel trackSearchPanel;

        private int searchResultDisplayHeight;
        private int searchResultDisplayOffset;
        private double searchResultBannerDisplayRatio;
        private int searchResultDisplayPadding;
        private Color searchResultPassiveColor;
        private Color searchResultActiveColor;

        #endregion

        #region Properties

        public List<SearchResult> TrackedSeries
        {
            get { return trackedSeries; }
        }
        public List<SearchResult> CurrentSearchResults
        {
            get { return currentSearchResults; }
        }

        public int InitialPanelIndex
        {
            get { return initialPanelIndex; }
            set { initialPanelIndex = value; }
        }
        public int LogoPanelIndex
        {
            get { return logoPanelIndex; }
            set { logoPanelIndex = value; }
        }
        public SearchResultSortCategory SearchTabSortCategory
        {
            get { return searchTabSortCategory; }
            set 
            { 
                searchTabSortCategory = value;
                foreach (SearchResult series in currentSearchResults)
                {
                    series.SortCategory = value;
                }
            }
        }
        public bool RestrictSearchResults
        {
            get { return restrictSearchResults; }
            set { restrictSearchResults = value; }
        }
        public int SearchResultLimit
        {
            get { return searchResultLimit; }
            set { searchResultLimit = value; }
        }

        public TrackSeriesPanel TrackSeriesPanel
        {
            get { return trackSeriesPanel; }
            set { trackSeriesPanel = value; }
        }
        public TrackSearchPanel TrackSearchPanel
        {
            get { return trackSearchPanel; }
            set { trackSearchPanel = value; }
        }

        public int SearchResultDisplayHeight
        {
            get { return searchResultDisplayHeight; }
            set { searchResultDisplayHeight = value; }
        }
        public int SearchResultDisplayOffset
        {
            get { return searchResultDisplayOffset; }
            set { searchResultDisplayOffset = value; }
        }
        public double SearchResultBannerDisplayRatio
        {
            get { return searchResultBannerDisplayRatio; }
            set { searchResultBannerDisplayRatio = value; }
        }
        public int SearchResultDisplayPadding
        {
            get { return searchResultDisplayPadding; }
            set { searchResultDisplayPadding = value; }
        }
        public Color SearchResultPassiveColor
        {
            get { return searchResultPassiveColor; }
            set { searchResultPassiveColor = value; }
        }
        public Color SearchResultActiveColor
        {
            get { return searchResultActiveColor; }
            set { searchResultActiveColor = value; }
        }

        #endregion

        #region Constructors

        public Controller()
        {
            trackedSeries = new List<SearchResult>();
            currentSearchResults = new List<SearchResult>();

            initialPanelIndex = 1;
            logoPanelIndex = 1;
            searchTabSortCategory = SearchResultSortCategory.Relevance;
            restrictSearchResults = true;
            searchResultLimit = 10;

            searchResultDisplayHeight = 80;
            searchResultDisplayOffset = 5;
            searchResultBannerDisplayRatio = 0.5;
            searchResultDisplayPadding = 10;
            searchResultPassiveColor = Color.FromArgb(63, Color.White);
            searchResultActiveColor = Color.FromArgb(127, Color.White);
        }

        #endregion

        #region Functions

        public void AddTrackedSeries(SearchResult series)
        {
            SearchResult s = series;
            s.SortCategory = SearchResultSortCategory.Alphabetic;
            trackedSeries.Add(s);

            trackSeriesPanel.AddTrackedSeries(s);
            trackSearchPanel.AddTrackedSeries(series);
        }

        public void RemoveTrackedSeries(SearchResult series)
        {
            foreach (SearchResult result in trackedSeries)
            {
                if (result.SeriesName == series.SeriesName)
                {
                    trackedSeries.Remove(result);
                    break;
                }
            }

            trackSeriesPanel.RemoveTrackedSeries(series);
            trackSearchPanel.RemoveTrackedSeries(series);
        }

        public void ClearSearchResults()
        {
            currentSearchResults.Clear();
        }

        public void AddSearchResult(SearchResult series, int relevance)
        {
            // Add the search result
            currentSearchResults.Add(series);
            currentSearchResults[currentSearchResults.Count - 1].Relevance = relevance;
            currentSearchResults[currentSearchResults.Count - 1].SortCategory = searchTabSortCategory;

            // Sort the current list of search results
            currentSearchResults.Sort();
        }

        #endregion
    }
}
